/*
 * Vectrics 
 * Created on Feb 1, 2004
 *
 */
package org.vectrics.common.graphics;


import javax.servlet.http.*;
import org.apache.log4j.Logger;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Font;
import java.awt.RenderingHints;


/**
 * @author mmoore
 */
public class TextGraphic extends Graphic {
	private transient static Logger log = Logger.getLogger(TextGraphic.class);
	
	private String text = "No text given";
	private int fontSize = 20;
	private String fontFace = "Helvetica";
	private int width = 300;
	private int height = 50;
	private Color background = Color.WHITE;
	boolean bold = false;
	
	public TextGraphic(HttpServletRequest request) {
		super();
		text = request.getParameter("text");
		String sizeParam = request.getParameter("size");
		if (sizeParam != null) {
			fontSize = Integer.parseInt(sizeParam);
		}
		String widthParam = request.getParameter("width");
		if (widthParam != null) {
			width = Integer.parseInt(widthParam);
		}
		String heightParam = request.getParameter("height");
		if (heightParam != null) {
			height = Integer.parseInt(heightParam);
		}
		String colorParam = request.getParameter("color");
		if (colorParam != null) {
		//	background = new Color(colorParam);
		}
		String boldParam = request.getParameter("bold");
		if (colorParam != null) {
			if ((boldParam != null) && (boldParam.toLowerCase().startsWith("f"))) {
				bold = true;
			}
		}
		
		/*
		Font font[] = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
		for (int i = 0; i < font.length; i++) {
			log.info("System font: " + font[i].getFontName());
		}
		*/	
				
	}
	
	public int getHeight() {
		return(height);
	}
	
	public int getWidth() {
		return(width);
	}
	
	public void doFill(Graphics2D g2) {
		log.debug("Adding anti-aliasing");
		RenderingHints AALIAS = new	RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.addRenderingHints(AALIAS);
				
		int fontWeight = Font.PLAIN;
		if (bold) {
			fontWeight = Font.BOLD;
		}
		Font font = new Font("Arial", fontWeight, fontSize);
		g2.setFont(font);
		int fontHeight = fontSize;
		g2.drawString(text, 0, fontHeight);
	}
	
}
